package cz.muni.fi.pa165.pokemon.util;

import cz.muni.fi.pa165.pokemon.dto.TrainerTO;
import cz.muni.fi.pa165.pokemon.entity.Trainer;
import cz.muni.fi.pa165.pokemon.dto.TrainerPosition;
import java.util.EnumMap;
import java.util.Map;
import org.dozer.CustomConverter;
import org.dozer.DozerBeanMapper;

/**
 *
 * @author Dusan Svancara
 */
public class EnumMapConverter implements CustomConverter {

        @Override
        public Object convert(Object o, Object o1, Class<?> type, Class<?> type1) {
            if (o1 == null) {
                return null;
            }
            DozerBeanMapper m = new DozerBeanMapper();
            //
            Map<TrainerPosition, Object> trainers = new EnumMap<>(TrainerPosition.class);
            Map<TrainerPosition, Object> old = (Map<TrainerPosition, Object>) o1;
            for (Map.Entry<TrainerPosition, Object> me : old.entrySet()) {
                if (me.getValue() instanceof TrainerTO) {
                    trainers.put(me.getKey(), m.map(me.getValue(), Trainer.class));
                } else {
                    trainers.put(me.getKey(), m.map(me.getValue(), TrainerTO.class));
                }
            }

            return trainers;
        }
    
    }
